﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Medianamik.Core
{
    /// <summary>
    /// Collection de NodeProperties associées à un NodeType
    /// </summary>
    public class NodeTypePropertyCollection : Collection<NodeTypeProperty>
    {
        #region .ctors

        /// <summary>
        /// Créée une instance de la classe, liée à un NodeType
        /// </summary>
        /// <param name="pOwnerType">Le NodeType propriétaire de la collection</param>
        internal NodeTypePropertyCollection(NodeType pOwnerType)
        {
            if (pOwnerType == null)
            {
                throw new ArgumentNullException("pOwnerType");
            }
            Owner = pOwnerType;
                        
        }

        #endregion

        /// <summary>
        /// Collection NodeTypeProperties à supprimer dans la BD
        /// </summary>
        private List<NodeTypeProperty> _removed = new List<NodeTypeProperty>();
        internal ReadOnlyCollection<NodeTypeProperty> Removed
        {
            get 
            {
                
                return _removed.AsReadOnly();
            }
        }

         /// <summary>
        /// Le NodeType propriétaire de la collection
        /// </summary>
        internal NodeType Owner
        {
            get;
            private set;
        }

        #region Overrides

        protected override void  SetItem(int index, NodeTypeProperty item)
        {
 	         throw new NotSupportedException("Cannot re-assign a NodeTypeProperty");                
        }
        protected override void  RemoveItem(int index)
        {
            var item = Items[index];
 	        base.RemoveItem(index);
            _removed.Add(item);

        }
        protected override void  ClearItems()
        {
            _removed.AddRange(this.Items); 
 	         base.ClearItems();
        }
        protected override void  InsertItem(int index, NodeTypeProperty item)
        {
            base.InsertItem(index, item);
        }

        #endregion

        public NodeTypeProperty this[Guid pId]
        {
            get
            {
                return this.Single(p => p != null && p.ID.Equals(pId));
            }
        }
		public NodeTypeProperty this[string pName]
		{
			get
			{
				if (string.IsNullOrEmpty(pName))
				{
					throw new ArgumentException("pName cannot be null or Empty");
				}
				var prop = this.SingleOrDefault(p => p.Name.Equals(pName, StringComparison.InvariantCultureIgnoreCase));
                if (prop == null)
				{
					throw new CoreException("No Property with the name " + pName + " was found.");
				}
				return prop;
			}
		}
        public bool Exists(string pName)
        {
            var prop = this.SingleOrDefault(p => p.Name.Equals(pName, StringComparison.InvariantCultureIgnoreCase));
            return prop != null;
        }


        public void AddRange(IEnumerable<NodeTypeProperty> pItems)
        {
            foreach (NodeTypeProperty item in pItems)
            {
                Add(item);
            }
        }

        public ReadOnlyCollection<NodeTypeProperty> AsReadOnly()
        {
            return new ReadOnlyCollection<NodeTypeProperty>(Items);
        }
               

       
    }
}
